package com.xoa.service.party;

import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.footprint.FootPrintMapper;
import com.xoa.dao.party.GridEveromentUserMapper;
import com.xoa.dao.party.GridEveromentcheckMapper;
import com.xoa.dao.party.GridHolidayMapper;
import com.xoa.model.department.Department;
import com.xoa.model.footprint.Foot;
import com.xoa.model.party.GridEventModel;
import com.xoa.model.party.GridEveromentUser;
import com.xoa.model.party.GridEveromentcheck;
import com.xoa.model.party.GridHoliday;
import com.xoa.model.users.Users;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by gsb on 2018/1/15.
 */
@Service
public class ThirdPartyService {

    @Resource
    private GridEveromentUserMapper gridEveromentUserMapper;

    @Resource
    private GridEveromentcheckMapper gridEveromentcheckMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private FootPrintMapper footPrintMapper;

    @Resource
    private GridHolidayMapper gridHolidayMapper;

    @Resource
    private ThirdPartyService thirdPartyService;

    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-15 下午16:26:51
     * 类介绍  : 社区   第三方情况
     * 构造参数:
     *
     */
    public ToJson<GridEveromentcheck> selectThirdParty(String time, Integer page,
                                                       Integer pageSize, boolean useFlag){
        ToJson<GridEveromentcheck> json = new ToJson<>();
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date1=sdf.parse(time);
            if(time.equals(DateFormat.getCurrentTime2())){
                List<GridEveromentcheck> gridEveromentcheckList = new ArrayList<>();
                Date date=sdf.parse(time);
                Calendar calendar  =  new GregorianCalendar();
                calendar.setTime(date);
                calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
                date=calendar.getTime();   //这个时间就是日期往后推一天的结果
                calendar.add(calendar.DATE,-1);
                Date date2=calendar.getTime();   //这个时间就是日期往前推一天的结果
                calendar.add(calendar.DATE,-2);
                Date date3=calendar.getTime();   //这个时间就是日期往前推一天的结果

                List<Department> departmentList = departmentMapper.getChDept(27);
                GridEveromentcheck gridEveromentcheck = null;
                    if (departmentList!=null && departmentList.size()>0){
                        for (Department department:departmentList){
                            gridEveromentcheck = new GridEveromentcheck();
                            Map<String, Object> map = new HashMap<String, Object>();//查询第三方派单数
                            map.put("startTime",date1);
                            map.put("endTime",date);
                            map.put("deptId",department.getDeptId());
                            GridEventModel gridEventModel = gridEveromentUserMapper.selectThirdParty(map);

                            gridEveromentcheck.setGridId(department.getDeptId().toString());
                            gridEveromentcheck.setGridName(department.getDeptName());
                            gridEveromentcheck.setDate(date1);
                            gridEveromentcheck.setThirdPartyNum(gridEventModel.getX());
                            Map<String, Object> map2 = new HashMap<String, Object>();//查询2日内未回复总数
                            map2.put("startTime",date2);
                            map2.put("endTime",date3);
                            map2.put("deptId",department.getDeptId());
                            map2.put("status",1);
                            GridEventModel gridEventModel2 = gridEveromentUserMapper.selectThirdParty(map2);
                            gridEveromentcheck.setNoReplyNum(gridEventModel2.getX());
                            int score = Integer.parseInt(gridEventModel.getX())+Integer.parseInt(gridEventModel2.getX());
                            score = score*(-5);
                            gridEveromentcheck.setThirdPartyScore(score+"");
                            gridEveromentcheckList.add(gridEveromentcheck);

                            /*Map<String, Object> map3 = new HashMap<String, Object>();//查询  该社区   在输入时间  是否已有记录
                            map3.put("gridId",department.getDeptId());
                            map3.put("newdate",date1);
                            List<GridEveromentcheck> gridEveromentcheckList = gridEveromentcheckMapper.selectEveromentCheck(map3);
                            if (gridEveromentcheckList!=null &&gridEveromentcheckList.size()>0){
                                gridEveromentcheck.setEid(gridEveromentcheckList.get(0).getEid());
                                gridEveromentcheckMapper.updateByPrimaryKeySelective(gridEveromentcheck);
                            }else {
                                gridEveromentcheckMapper.insertSelective(gridEveromentcheck);
                            }*/
                        }
                    }
                        json.setObj(gridEveromentcheckList);
                        json.setFlag(0);
                        json.setMsg("ok");
            }else {
                Map<String, Object> map4 = new HashMap<String, Object>();
                map4.put("newdate",date1);
                List<GridEveromentcheck> gridEveromentcheck1 = gridEveromentcheckMapper.selectEveromentCheck(map4);
                json.setObj(gridEveromentcheck1);
                json.setTotleNum(gridEveromentcheckMapper.selectEveromentCount(map4));
                json.setFlag(0);
                json.setMsg("ok");

            }

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-15 下午16:46:51
     * 类介绍  :查询人员 每天分数
     * 构造参数:
     *
     */

    public ToJson<GridEveromentUser> selectDayUserScore(String time, Integer deptId, Integer page, Integer pageSize,
                                                        boolean useFlag){
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date1=sdf.parse(time);
//            Map<String, Object> map = new HashMap<String, Object>();
//            map.put("gridId",deptId);
//            map.put("date",date1);
//            List<GridEveromentUser> gridEveromentUserList = gridEveromentUserMapper.selectEveromentUser(map);
//            if (gridEveromentUserList!=null && gridEveromentUserList.size()>0){
//                for (GridEveromentUser gridEveromentUser:gridEveromentUserList){
//                    int todayScore = 0;
//                    if (gridEveromentUser.getM1()==null || "".equals(gridEveromentUser.getM1())){
//                        todayScore = todayScore + 0;
//                    }else {
//                        todayScore =todayScore + Integer.parseInt(gridEveromentUser.getM1());
//                    }
//                    if (gridEveromentUser.getCheckScore()==null || "".equals(gridEveromentUser.getCheckScore())){
//                        todayScore = todayScore + 0;
//                    }else {
//                        todayScore =todayScore + Integer.parseInt(gridEveromentUser.getCheckScore());
//                    }
//                    if (gridEveromentUser.getRepoetScore()==null || "".equals(gridEveromentUser.getRepoetScore())){
//                        todayScore = todayScore + 0;
//                    }else {
//                        todayScore =todayScore + Integer.parseInt(gridEveromentUser.getRepoetScore());
//                    }
//
//                    GridEveromentUser gridEveromentUser1 = new GridEveromentUser();
//                    gridEveromentUser1.setMid(gridEveromentUser.getMid());
//                    gridEveromentUser1.setTodayScore(todayScore+"");
//                    gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser1);
//                }
//            }
            Map<String, Object> map1 = new HashMap<String, Object>();
            PageParams pageParams=new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            map1.put("page",pageParams);
            map1.put("gridId",deptId);
            map1.put("date",date1);
            List<GridEveromentUser> gridEveromentUserList1 = gridEveromentUserMapper.selectEveromentUser(map1);
            json.setObj(gridEveromentUserList1);
            json.setTotleNum(gridEveromentUserMapper.queryCount(map1));
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-15 下午17:46:51
     * 类介绍  :社区 每天分数
     * 构造参数:
     *
     */
    public ToJson<GridEveromentcheck> selectDayGridScore(String time){
        ToJson<GridEveromentcheck> json = new ToJson<>();
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date1=sdf.parse(time);
           /* if(time.equals(DateFormat.getCurrentTime())){
                List<GridEveromentcheck> gridEveromentcheckList1 = new ArrayList<>();
                 Map<String, Object> map1 = new HashMap<String, Object>();
                 map1.put("newdate",date1);
                 List<GridEveromentcheck> gridEveromentcheckList = gridEveromentcheckMapper.selectEveromentCheck(map1);
                 if (gridEveromentcheckList != null && gridEveromentcheckList.size()>0){
                    for (GridEveromentcheck gridEveromentcheck:gridEveromentcheckList){
                        GridEveromentcheck gridEveromentcheck1 = new GridEveromentcheck();
                        int score = 0;
                        if (gridEveromentcheck.getAttendScore() == null || "".equals(gridEveromentcheck.getAttendScore())){
                            score = score+0;
                        }else {
                            score = score+Integer.parseInt(gridEveromentcheck.getAttendScore());
                        }
                        if (gridEveromentcheck.getCheckScore() == null || "".equals(gridEveromentcheck.getCheckScore())){
                            score = score+0;
                        }else {
                            score = score+Integer.parseInt(gridEveromentcheck.getCheckScore());
                        }
                        if (gridEveromentcheck.getRepoetScore() == null || "".equals(gridEveromentcheck.getRepoetScore())){
                            score = score+0;
                        }else {
                            score = score+Integer.parseInt(gridEveromentcheck.getRepoetScore());
                        }
                        if (gridEveromentcheck.getThirdPartyScore() == null || "".equals(gridEveromentcheck.getThirdPartyScore())){
                            score = score+0;
                        }else {
                            score = score+Integer.parseInt(gridEveromentcheck.getThirdPartyScore());
                        }
    //                    int score = Integer.parseInt(gridEveromentcheck.getAttendScore())+ Integer.parseInt(gridEveromentcheck.getCheckScore())
    //                            + Integer.parseInt(gridEveromentcheck.getRepoetScore())+ Integer.parseInt(gridEveromentcheck.getThirdPartyScore());

                        gridEveromentcheck.setEid(gridEveromentcheck.getEid());
                        gridEveromentcheck.setTodayScore(score+"");
                        gridEveromentcheckList1.add(gridEveromentcheck);
                        //gridEveromentcheckMapper.updateByPrimaryKeySelective(gridEveromentcheck1);
                    }

                }
                json.setObj(gridEveromentcheckList1);
                json.setFlag(0);
                json.setMsg("ok");

            }*/
                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("newdate",date1);
                List<GridEveromentcheck> gridEveromentcheckList1 = gridEveromentcheckMapper.selectEveromentCheck(map2);
                json.setObj(gridEveromentcheckList1);
                json.setTotleNum(gridEveromentcheckMapper.selectEveromentCount(map2));
                json.setFlag(0);
                json.setMsg("ok");

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }

    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-15 下午16:46:51
     * 类介绍  :人员 总分
     * 构造参数:
     *
     */
        public ToJson<GridEveromentUser> selectYearUserScore(Integer deptId, Integer page, Integer pageSize,
                                                             boolean useFlag){
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {
            Calendar currCal=Calendar.getInstance();
            int currentYear = currCal.get(Calendar.YEAR);
            Date startDate =  getYearFirst(currentYear); //一年 开始时间

            Calendar currCal1=Calendar.getInstance();//  当前年  最后一天
            int currentYear1 = currCal1.get(Calendar.YEAR);
            Date endDate = getYearLast(currentYear1);
            Map<String, Object> map = new HashMap<String, Object>();

            map.put("startTime",startDate);
            map.put("endTime",endDate);
            map.put("gridId",deptId);
            Map<String, Object> map1 = new HashMap<String, Object>();
            PageParams pageParams=new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            map1.put("page",pageParams);
            map1.put("deptId",deptId);
            List<Users> usersList1 = gridEveromentUserMapper.selectUser(deptId);
            List<Users> usersList = gridEveromentUserMapper.selectUserPage(map1);
            int a1=0;//签到总分
            int a2=0;//巡检记录总分
            int a3=0;//网格中心总分
            int a4=0;//总分
            String gridName = null;
            GridEveromentUser gridEveromentUser1 = null;
            List<GridEveromentUser> gridEveromentUserList1 = new ArrayList<GridEveromentUser>();
            if (usersList!=null && usersList.size()>0){
                for (Users users :usersList){

                    map.put("gridUserId",users.getUserId());
                    List<GridEveromentUser> gridEveromentUserList = gridEveromentUserMapper.selectUserScore(map);
                    if (gridEveromentUserList!=null && gridEveromentUserList.size()>0){
                        for (GridEveromentUser gridEveromentUser:gridEveromentUserList){
                            if (gridEveromentUser.getM1()==null ||  "".equals(gridEveromentUser.getM1())){
                               a1=a1+0;
                            }else {
                                a1 = a1+Integer.parseInt(gridEveromentUser.getM1());
                            }
                            if (gridEveromentUser.getCheckScore()==null ||  "".equals(gridEveromentUser.getCheckScore())){
                                a2=a2+0;
                            }else {
                                a2 = a2+Integer.parseInt(gridEveromentUser.getCheckScore());
                            }
                            if (gridEveromentUser.getRepoetScore()==null || "".equals(gridEveromentUser.getRepoetScore())){
                                a3=a3+0;
                            }else {
                                a3 = a3+Integer.parseInt(gridEveromentUser.getRepoetScore());
                            }

                            gridName = gridEveromentUser.getGridName();
                        }
                        a4 = a1+a2+a3+1000;
                        gridEveromentUser1 = new GridEveromentUser();
                        gridEveromentUser1.setAttendAll(a1);
                        gridEveromentUser1.setCheckAll(a2);
                        gridEveromentUser1.setGridCenterAll(a3);
                        gridEveromentUser1.setScoreAll(a4);
                        gridEveromentUser1.setGridName(gridName);
                        gridEveromentUser1.setGridUserName(users.getUserName());
                        gridEveromentUserList1.add(gridEveromentUser1);
                    }

                }
            }
            json.setObj(gridEveromentUserList1);
            json.setTotleNum(usersList1.size());
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }

    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-15 下午16:46:51
     * 类介绍  :社区 总分
     * 构造参数:
     *
     */
    public ToJson<GridEveromentcheck> selectYearScore(){
        ToJson<GridEveromentcheck> json = new ToJson<>();
        try {

            Calendar currCal=Calendar.getInstance();
            int currentYear = currCal.get(Calendar.YEAR);
            Date startDate =  getYearFirst(currentYear); //一年 开始时间

            Calendar currCal1=Calendar.getInstance();//  当前年  最后一天
            int currentYear1 = currCal1.get(Calendar.YEAR);
            Date endDate = getYearLast(currentYear1);
            List<Department> departmentList = departmentMapper.getChDept(27);
            GridEveromentcheck gridEveromentcheck = null;
            List<GridEveromentcheck> gridEveromentcheckList = new ArrayList<GridEveromentcheck>();
            if (departmentList!= null && departmentList.size()>0){
                for (Department department:departmentList){
                    int a1=0;//签到总分
                    int a2=0;//巡检记录总分
                    int a3=0;//第三方总分
                    int a4=0;//网格中心总分
                    int a5=0;//总分
                    gridEveromentcheck = new GridEveromentcheck();
                    Map<String, Object> map = new HashMap<String, Object>();
                    if (2018 == currentYear){
                        map.put("startTime","2018-03-08");
                    }else {
                        map.put("startTime",startDate);
                    }
                    map.put("endTime",endDate);
                    map.put("gridId",department.getDeptId());
                    List<GridEveromentcheck> gridEveromentcheckList1 = gridEveromentcheckMapper.selectYearCheck(map);
                    if (gridEveromentcheckList1 !=null && gridEveromentcheckList1.size()>0){
                        for (GridEveromentcheck gridEveromentcheck1: gridEveromentcheckList1){
                            if (gridEveromentcheck1.getAttendScore() ==null ||"".equals(gridEveromentcheck1.getAttendScore())){
                                a1=a1+0;
                            }else {
                                a1 = a1+Integer.parseInt(gridEveromentcheck1.getAttendScore());
                            }
                            if (gridEveromentcheck1.getCheckScore() ==null ||"".equals(gridEveromentcheck1.getCheckScore())){
                                a2=a2+0;
                            }else {
                                a2 = a2+Integer.parseInt(gridEveromentcheck1.getCheckScore());
                            }
                            if (gridEveromentcheck1.getThirdPartyScore() ==null ||"".equals(gridEveromentcheck1.getThirdPartyScore())){
                                a3=a3+0;
                            }else {
                                a3 = a3+Integer.parseInt(gridEveromentcheck1.getThirdPartyScore());
                            }
                            if (gridEveromentcheck1.getRepoetScore() ==null ||"".equals(gridEveromentcheck1.getRepoetScore())){
                                a4=a4+0;
                            }else {
                                a4 = a4+Integer.parseInt(gridEveromentcheck1.getRepoetScore());
                            }

                        }
                    }
                    a5 = a1+a2+a3+a4+1000;
                    gridEveromentcheck.setAttendAll(a1);
                    gridEveromentcheck.setCheckAll(a2);
                    gridEveromentcheck.setThirdAll(a3);
                    gridEveromentcheck.setGridCenterAll(a4);
                    gridEveromentcheck.setScoreAll(a5);
                    gridEveromentcheck.setGridId(department.getDeptId().toString());
                    gridEveromentcheck.setGridName(department.getDeptName());
                    gridEveromentcheckList.add(gridEveromentcheck);
                }
            }
            json.setObj(gridEveromentcheckList);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        return currYearLast;
    }

    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-16 上午9:45:51
     * 类介绍  : 添加   社区签到 情况
     * 构造参数:
     *
     */
    public ToJson<GridEveromentcheck> insertGridAttend(){
        ToJson<GridEveromentcheck> json = new ToJson<>();
        try {
            Date d1 = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String time = sdf.format(d1);
            /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String time = "2018-01-25";*/
            Date date=sdf.parse(time);
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);
            boolean flag = false;
            int key = 1;
            if(ca.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ||
                    ca.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
                flag = true;
            }
            if (flag== true){
                key=0;
            }
            List<Department> departmentList = departmentMapper.getChDept(27);
            GridEveromentcheck gridEveromentcheck = null;

            if (departmentList!=null && departmentList.size()>0){
                for (Department department:departmentList){
                    gridEveromentcheck = new GridEveromentcheck();
                    gridEveromentcheck.setGridId(department.getDeptId().toString());
                    gridEveromentcheck.setGridName(department.getDeptName());

                    gridEveromentcheck.setDate(date);
                    ToJson<GridEveromentUser> gridEveromentUserToJson = thirdPartyService.selectCheck(time, key+"",
                            department.getDeptId());
                    List<GridEveromentUser> gridEveromentUserList = gridEveromentUserToJson.getObj();
                    int count = 0;
                    int score = 0;
                    //查询前端传入时间是否在节假日范围内
                    Map<String, Object> map1 = new HashMap<String, Object>();
                    map1.put("newTime",time);
                    List<GridHoliday> gridHolidayList =gridHolidayMapper.selectHoliday(map1);
                    int userNum = gridEveromentUserMapper.selectCount(department.getDeptId());
                    gridEveromentcheck.setGridNum(userNum+"");
                    if(gridEveromentUserList!=null && gridEveromentUserList.size()>0){
                        for (GridEveromentUser gridEveromentUser:gridEveromentUserList){
                            if ("1".equals(gridEveromentUser.getM1())){
                                count=count+1;
                            }
                        }
                    }
                    if (gridHolidayList!=null && gridHolidayList.size()>0){
                        gridEveromentcheck.setAttendScore("0");
                    }else {
//                        score = count - userNum;
                        score = count;
                        gridEveromentcheck.setAttendScore(score+"");
                    }
                    gridEveromentcheck.setAttendNum(count+"");
                    gridEveromentcheck.setAttendScore(score+"");
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("gridId",department.getDeptId());
                    map.put("newdate",date);
                    List<GridEveromentcheck> gridEveromentcheckList = gridEveromentcheckMapper.selectEveromentCheck(map);
                    if (gridEveromentcheckList!=null &&gridEveromentcheckList.size()>0){
                        gridEveromentcheck.setEid(gridEveromentcheckList.get(0).getEid());
                        gridEveromentcheckMapper.updateByPrimaryKeySelective(gridEveromentcheck);
                    }
//                    else {
//                        gridEveromentcheckMapper.insertSelective(gridEveromentcheck);
//                    }



                }
            }
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    public ToJson<GridEveromentcheck> insertGridAttend1(String DateTime){
        ToJson<GridEveromentcheck> json = new ToJson<>();
        try {
//            Date d1 = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            String time = sdf.format(d1);
           /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String time = "2018-01-25";*/
            Date date=sdf.parse(DateTime);
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);
            boolean flag = false;
            int key = 1;
            if(ca.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ||
                    ca.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
                flag = true;
            }
            if (flag== true){
                key=0;
            }
            List<Department> departmentList = departmentMapper.getChDept(27);
            GridEveromentcheck gridEveromentcheck = null;

            if (departmentList!=null && departmentList.size()>0){
                for (Department department:departmentList){
                    gridEveromentcheck = new GridEveromentcheck();
                    gridEveromentcheck.setGridId(department.getDeptId().toString());
                    gridEveromentcheck.setGridName(department.getDeptName());

                    gridEveromentcheck.setDate(date);
                    ToJson<GridEveromentUser> gridEveromentUserToJson = thirdPartyService.selectCheck(DateTime, key+"",
                            department.getDeptId());
                    List<GridEveromentUser> gridEveromentUserList = gridEveromentUserToJson.getObj();
                    int count = 0;
                    int score = 0;
                    //查询前端传入时间是否在节假日范围内
                    Map<String, Object> map1 = new HashMap<String, Object>();
                    map1.put("newTime",DateTime);
                    List<GridHoliday> gridHolidayList =gridHolidayMapper.selectHoliday(map1);
                    int userNum = gridEveromentUserMapper.selectCount(department.getDeptId());
                    gridEveromentcheck.setGridNum(userNum+"");
                    if(gridEveromentUserList!=null && gridEveromentUserList.size()>0){
                        for (GridEveromentUser gridEveromentUser:gridEveromentUserList){
                            if ("1".equals(gridEveromentUser.getM1())){
                                count=count+1;
                            }
                        }
                    }
                    if (gridHolidayList!=null && gridHolidayList.size()>0){
                        gridEveromentcheck.setAttendScore("0");
                    }else {
                        score = count - userNum;
                        gridEveromentcheck.setAttendScore(score+"");
                    }
                    gridEveromentcheck.setAttendNum(count+"");
                    gridEveromentcheck.setAttendScore(score+"");
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("gridId",department.getDeptId());
                    map.put("newdate",date);
                    List<GridEveromentcheck> gridEveromentcheckList = gridEveromentcheckMapper.selectEveromentCheck(map);
                    if (gridEveromentcheckList!=null &&gridEveromentcheckList.size()>0){
                        gridEveromentcheck.setEid(gridEveromentcheckList.get(0).getEid());
                        gridEveromentcheckMapper.updateByPrimaryKeySelective(gridEveromentcheck);
                        for(GridEveromentcheck gridEveromentcheck1:gridEveromentcheckList){
                            if(!gridEveromentcheck1.getEid().equals(gridEveromentcheckList.get(0).getEid())){
                                gridEveromentcheckMapper.deleteByPrimaryKey(gridEveromentcheck1.getEid());
                            }
                        }
                    }else {
                        gridEveromentcheckMapper.insertSelective(gridEveromentcheck);
                    }



                }
            }
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }

    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-16 上午9:45:51
     * 类介绍  : 添加   人员签到 情况
     * 构造参数:
     *
     */
    public ToJson<GridEveromentUser> selectCheck(String time, String key, Integer deptId){
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {
            Integer begin = DateFormat.getDateTime(time);// 开始时间戳
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date=sdf.parse(time);
            Calendar calendar  =  new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
            date=calendar.getTime();   //这个时间就是日期往后推一天的结果
            String T = DateFormat.getDatestr(date);
            Integer end = DateFormat.getDateTime(T); // 结束时间戳
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("begin",begin);
            map.put("end",end);
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("newTime",time);
            //查询前端传入时间是否在节假日范围内
            List<GridHoliday> gridHolidayList =gridHolidayMapper.selectHoliday(map1);
            GridEveromentUser gridEveromentUser = null;
            Date date1=sdf.parse(time);

            List<Users> usersList = gridEveromentUserMapper.selectUser(deptId);
            if (usersList!=null && usersList.size()>0){
                for (Users user:usersList){
                    gridEveromentUser = new GridEveromentUser();
                    gridEveromentUser.setGridId(user.getDeptId().toString());
                    gridEveromentUser.setGridName(user.getDeptName());
                    gridEveromentUser.setGridUserId(user.getUserId());
                    gridEveromentUser.setGridUserName(user.getUserName());
                    gridEveromentUser.setDate(date1);
                    map.put("uid",user.getUid());
                 /*   List<Foot> footList = footPrintMapper.getUserFootPrint(map);
                    //判断 输入时间  那天  有无签到
                    if (footList!=null && footList.size()>0){
                        Integer fTime=footList.get(0).getUpdateTime();
                        Long l=(new Long(fTime.toString()))*1000; ;
                        Date d = new Date(l);
                        String res = sdf.format(d);
                        gridEveromentUser.setAttendNum(res);
                        gridEveromentUser.setAttendScore(footList.get(0).getLocation());
                        if("0".equals(key)){
                            gridEveromentUser.setM1("0");
                        }else if (gridHolidayList !=null && gridHolidayList.size()>0){
                            gridEveromentUser.setM1("0");
                        }else {
                            gridEveromentUser.setM1("1");
                        }
                    }else {
                        gridEveromentUser.setM1("0");
                    }*/
                    Map<String, Object> map2 = new HashMap<String, Object>();
                    map2.put("date",date1);
                    map2.put("gridUserId",user.getUserId());
                    //  查询输入时间  是否在环保考核人员表里有记录
                    //有就执行更新操作  没有就新建
                    List<GridEveromentUser> gridEveromentUserList = gridEveromentUserMapper.selectEveromentUser(map2);
                    if(gridEveromentUserList!=null && gridEveromentUserList.size()>0){
                        gridEveromentUser.setMid(gridEveromentUserList.get(0).getMid());
                        gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser);

                    }else {
                        gridEveromentUserMapper.insertSelective(gridEveromentUser);
                    }
                }
            }
            //查询 输入时间那天  社区人员的签到情况
            Map<String, Object> map3 = new HashMap<String, Object>();

                map3.put("gridId",deptId);
                map3.put("date",date1);
                List<GridEveromentUser> gridEveromentUserList1 = gridEveromentUserMapper.selectEveromentUser(map3);
                json.setObj(gridEveromentUserList1);
                json.setTotleNum(gridEveromentUserMapper.queryCount(map3));
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }


/**
 *
 * 创建作者:   季佳伟
 * 创建日期:   2018-1-16 上午9:45:51
 * 类介绍  : 添加   每天社区   第三方情况
 * 构造参数:
 *
 */
    public ToJson<GridEveromentcheck> insertThird(){
    ToJson<GridEveromentcheck> json = new ToJson<>();
    try {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(d);
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = "2018-01-25";*/
        Date date1=sdf.parse(dateNowStr);
        Date date=sdf.parse(dateNowStr);
        Calendar calendar  =  new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
        date=calendar.getTime();   //这个时间就是日期往后推一天的结果
        calendar.add(calendar.DATE,-1);
        Date date2=calendar.getTime();   //这个时间就是日期往前推一天的结果
        calendar.add(calendar.DATE,-2);
        Date date3=calendar.getTime();   //这个时间就是日期往前推一天的结果

        List<Department> departmentList = departmentMapper.getChDept(27);
        GridEveromentcheck gridEveromentcheck = null;
        if (departmentList!=null && departmentList.size()>0){
            for (Department department:departmentList){
                gridEveromentcheck = new GridEveromentcheck();
                Map<String, Object> map = new HashMap<String, Object>();//查询第三方派单数
                map.put("startTime",date1);
                map.put("endTime",date);
                map.put("deptId",department.getDeptId());
                GridEventModel gridEventModel = gridEveromentUserMapper.selectThirdParty(map);

                gridEveromentcheck.setGridId(department.getDeptId().toString());
                gridEveromentcheck.setGridName(department.getDeptName());
                gridEveromentcheck.setDate(date1);
                gridEveromentcheck.setThirdPartyNum(gridEventModel.getX());
                Map<String, Object> map2 = new HashMap<String, Object>();//查询2日内未回复总数
                map2.put("startTime",date2);
                map2.put("endTime",date3);
                map2.put("deptId",department.getDeptId());
                map2.put("status",1);
                GridEventModel gridEventModel2 = gridEveromentUserMapper.selectThirdParty(map2);
                gridEveromentcheck.setNoReplyNum(gridEventModel2.getX());
                int score = Integer.parseInt(gridEventModel.getX())+Integer.parseInt(gridEventModel2.getX());
                score = score*(-5);
                gridEveromentcheck.setThirdPartyScore(score+"");
                Map<String, Object> map3 = new HashMap<String, Object>();//查询  该社区   在输入时间  是否已有记录
                map3.put("gridId",department.getDeptId());
                map3.put("newdate",date1);
                List<GridEveromentcheck> gridEveromentcheckList = gridEveromentcheckMapper.selectEveromentCheck(map3);
                if (gridEveromentcheckList!=null &&gridEveromentcheckList.size()>0){
                    gridEveromentcheck.setEid(gridEveromentcheckList.get(0).getEid());
                    gridEveromentcheckMapper.updateByPrimaryKeySelective(gridEveromentcheck);
                }
//                else {
//                    gridEveromentcheckMapper.insertSelective(gridEveromentcheck);
//                }
            }
        }
    }catch (Exception e){
        e.printStackTrace();
        json.setFlag(1);
        json.setMsg(e.getMessage());
    }
    return json;
}
    public ToJson<GridEveromentcheck> insertThird1(String DateTime){
        ToJson<GridEveromentcheck> json = new ToJson<>();
        try {
           // Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          //  String dateNowStr = sdf.format(d);
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = "2018-01-25";*/
            Date date1=sdf.parse(DateTime);
            Date date=sdf.parse(DateTime);
            Calendar calendar  =  new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
            date=calendar.getTime();   //这个时间就是日期往后推一天的结果
            calendar.add(calendar.DATE,-1);
            Date date2=calendar.getTime();   //这个时间就是日期往前推一天的结果
            calendar.add(calendar.DATE,-2);
            Date date3=calendar.getTime();   //这个时间就是日期往前推一天的结果

            List<Department> departmentList = departmentMapper.getChDept(27);
            GridEveromentcheck gridEveromentcheck = null;
            if (departmentList!=null && departmentList.size()>0){
                for (Department department:departmentList){
                    gridEveromentcheck = new GridEveromentcheck();
                    Map<String, Object> map = new HashMap<String, Object>();//查询第三方派单数
                    map.put("startTime",date1);
                    map.put("endTime",date);
                    map.put("deptId",department.getDeptId());
                    GridEventModel gridEventModel = gridEveromentUserMapper.selectThirdParty(map);

                    gridEveromentcheck.setGridId(department.getDeptId().toString());
                    gridEveromentcheck.setGridName(department.getDeptName());
                    gridEveromentcheck.setDate(date1);
                    gridEveromentcheck.setThirdPartyNum(gridEventModel.getX());
                    Map<String, Object> map2 = new HashMap<String, Object>();//查询2日内未回复总数
                    map2.put("startTime",date2);
                    map2.put("endTime",date3);
                    map2.put("deptId",department.getDeptId());
                    map2.put("status",1);
                    GridEventModel gridEventModel2 = gridEveromentUserMapper.selectThirdParty(map);
                    gridEveromentcheck.setNoReplyNum(gridEventModel2.getX());
                    int score = Integer.parseInt(gridEventModel.getX())+Integer.parseInt(gridEventModel2.getX());
                    score = score*(-5);
                    gridEveromentcheck.setThirdPartyScore(score+"");
                    Map<String, Object> map3 = new HashMap<String, Object>();//查询  该社区   在输入时间  是否已有记录
                    map3.put("gridId",department.getDeptId());
                    map3.put("newdate",date1);
                    List<GridEveromentcheck> gridEveromentcheckList = gridEveromentcheckMapper.selectEveromentCheck(map3);
                    if (gridEveromentcheckList!=null &&gridEveromentcheckList.size()>0){
                        gridEveromentcheck.setEid(gridEveromentcheckList.get(0).getEid());
                        gridEveromentcheckMapper.updateByPrimaryKeySelective(gridEveromentcheck);
                        for(GridEveromentcheck gridEveromentcheck1:gridEveromentcheckList){
                            if(!gridEveromentcheck1.getEid().equals(gridEveromentcheckList.get(0).getEid())){
                                gridEveromentcheckMapper.deleteByPrimaryKey(gridEveromentcheck1.getEid());
                            }
                        }
                    }else {
                        gridEveromentcheckMapper.insertSelective(gridEveromentcheck);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }


/**
 *
 * 创建作者:   季佳伟
 * 创建日期:   2018-1-16 上午9:45:51
 * 类介绍  : 汇总   人员 每天分数
 * 构造参数:
 *
 */
public ToJson<GridEveromentUser> insertDayUserScore(){
    ToJson<GridEveromentUser> json = new ToJson<>();
    try {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(d);
       /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = "2018-01-25";*/
        Date date1=sdf.parse(dateNowStr);
        List<Department> departmentList = departmentMapper.getChDept(27);
        if (departmentList!=null && departmentList.size()>0){
            for (Department department:departmentList){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("gridId",department.getDeptId());
                map.put("date",date1);
                List<GridEveromentUser> gridEveromentUserList = gridEveromentUserMapper.selectEveromentUser(map);
                if (gridEveromentUserList!=null && gridEveromentUserList.size()>0){
                    for (GridEveromentUser gridEveromentUser:gridEveromentUserList){
                        int todayScore = 0;
                        if (gridEveromentUser.getM1()==null || "".equals(gridEveromentUser.getM1())){
                            todayScore = todayScore + 0;
                        }else {
                            todayScore =todayScore + Integer.parseInt(gridEveromentUser.getM1());
                        }
                        if (gridEveromentUser.getCheckScore()==null || "".equals(gridEveromentUser.getCheckScore())){
                            todayScore = todayScore + 0;
                        }else {
                            todayScore =todayScore + Integer.parseInt(gridEveromentUser.getCheckScore());
                        }
                        if (gridEveromentUser.getRepoetScore()==null || "".equals(gridEveromentUser.getRepoetScore())){
                            todayScore = todayScore + 0;
                        }else {
                            todayScore =todayScore + Integer.parseInt(gridEveromentUser.getRepoetScore());
                        }

                        GridEveromentUser gridEveromentUser1 = new GridEveromentUser();
                        gridEveromentUser1.setMid(gridEveromentUser.getMid());
                        gridEveromentUser1.setTodayScore(todayScore+"");
                        gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser1);
                    }
                }
            }
        }

    }catch (Exception e){
        e.printStackTrace();
        json.setFlag(1);
        json.setMsg(e.getMessage());
    }
    return json;
}
    public ToJson<GridEveromentUser> insertDayUserScore1(String DateTime){
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {
//        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
       //String dateNowStr = sdf.format(d);
           /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateNowStr = "2018-01-25";*/
            Date date1=sdf.parse(DateTime);
            List<Department> departmentList = departmentMapper.getChDept(27);
            if (departmentList!=null && departmentList.size()>0){
                for (Department department:departmentList){
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("gridId",department.getDeptId());
                    map.put("date",date1);
                    List<GridEveromentUser> gridEveromentUserList = gridEveromentUserMapper.selectEveromentUser(map);
                    if (gridEveromentUserList!=null && gridEveromentUserList.size()>0){
                        for (GridEveromentUser gridEveromentUser:gridEveromentUserList){
                            int todayScore = 0;
                            if (gridEveromentUser.getM1()==null || "".equals(gridEveromentUser.getM1())){
                                todayScore = todayScore + 0;
                            }else {
                                todayScore =todayScore + Integer.parseInt(gridEveromentUser.getM1());
                            }
                            if (gridEveromentUser.getCheckScore()==null || "".equals(gridEveromentUser.getCheckScore())){
                                todayScore = todayScore + 0;
                            }else {
                                todayScore =todayScore + Integer.parseInt(gridEveromentUser.getCheckScore());
                            }
                            if (gridEveromentUser.getRepoetScore()==null || "".equals(gridEveromentUser.getRepoetScore())){
                                todayScore = todayScore + 0;
                            }else {
                                todayScore =todayScore + Integer.parseInt(gridEveromentUser.getRepoetScore());
                            }

                            GridEveromentUser gridEveromentUser1 = new GridEveromentUser();
                            gridEveromentUser1.setMid(gridEveromentUser.getMid());
                            gridEveromentUser1.setTodayScore(todayScore+"");
                            gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser1);
                        }
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
/**
 *
 * 创建作者:   季佳伟
 * 创建日期:   2018-1-16 上午9:45:51
 * 类介绍  : 汇总   社区 每天分数
 * 构造参数:
 *
 */
public ToJson<GridEveromentcheck> insertDayGridScore(){
    ToJson<GridEveromentcheck> json = new ToJson<>();
    try {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(d);
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = "2018-01-25";*/
        Date date1=sdf.parse(dateNowStr);
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("newdate",date1);
        List<GridEveromentcheck> gridEveromentcheckList = gridEveromentcheckMapper.selectEveromentCheck(map1);
        if (gridEveromentcheckList != null && gridEveromentcheckList.size()>0){
            for (GridEveromentcheck gridEveromentcheck:gridEveromentcheckList){
                GridEveromentcheck gridEveromentcheck1 = new GridEveromentcheck();
                int score = 0;
                if (gridEveromentcheck.getAttendScore() == null || "".equals(gridEveromentcheck.getAttendScore())){
                    score = score+0;
                }else {
                    score = score+Integer.parseInt(gridEveromentcheck.getAttendScore());
                }
                if (gridEveromentcheck.getCheckScore() == null || "".equals(gridEveromentcheck.getCheckScore())){
                    score = score+0;
                }else {
                    score = score+Integer.parseInt(gridEveromentcheck.getCheckScore());
                }
                if (gridEveromentcheck.getRepoetScore() == null || "".equals(gridEveromentcheck.getRepoetScore())){
                    score = score+0;
                }else {
                    score = score+Integer.parseInt(gridEveromentcheck.getRepoetScore());
                }
                if (gridEveromentcheck.getThirdPartyScore() == null || "".equals(gridEveromentcheck.getThirdPartyScore())){
                    score = score+0;
                }else {
                    score = score+Integer.parseInt(gridEveromentcheck.getThirdPartyScore());
                }

                gridEveromentcheck1.setEid(gridEveromentcheck.getEid());
                gridEveromentcheck1.setTodayScore(score+"");
                gridEveromentcheckMapper.updateByPrimaryKeySelective(gridEveromentcheck1);
            }
        }
    }catch (Exception e){
        e.printStackTrace();
        json.setFlag(1);
        json.setMsg(e.getMessage());
    }
    return json;
}
    public ToJson<GridEveromentcheck> insertDayGridScore1(String DateTime){
        ToJson<GridEveromentcheck> json = new ToJson<>();
        try {
//        Date d = new Date();
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String dateNowStr = sdf.format(d);
           /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateNowStr = "2018-01-25";*/
            Date date1=sdf.parse(DateTime);
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("newdate",date1);
            List<GridEveromentcheck> gridEveromentcheckList = gridEveromentcheckMapper.selectEveromentCheck(map1);
            if (gridEveromentcheckList != null && gridEveromentcheckList.size()>0){
                for (GridEveromentcheck gridEveromentcheck:gridEveromentcheckList){
                    GridEveromentcheck gridEveromentcheck1 = new GridEveromentcheck();
                    int score = 0;
                    if (gridEveromentcheck.getAttendScore() == null || "".equals(gridEveromentcheck.getAttendScore())){
                        score = score+0;
                    }else {
                        score = score+Integer.parseInt(gridEveromentcheck.getAttendScore());
                    }
                    if (gridEveromentcheck.getCheckScore() == null || "".equals(gridEveromentcheck.getCheckScore())){
                        score = score+0;
                    }else {
                        score = score+Integer.parseInt(gridEveromentcheck.getCheckScore());
                    }
                    if (gridEveromentcheck.getRepoetScore() == null || "".equals(gridEveromentcheck.getRepoetScore())){
                        score = score+0;
                    }else {
                        score = score+Integer.parseInt(gridEveromentcheck.getRepoetScore());
                    }
                    if (gridEveromentcheck.getThirdPartyScore() == null || "".equals(gridEveromentcheck.getThirdPartyScore())){
                        score = score+0;
                    }else {
                        score = score+Integer.parseInt(gridEveromentcheck.getThirdPartyScore());
                    }

                    gridEveromentcheck1.setEid(gridEveromentcheck.getEid());
                    gridEveromentcheck1.setTodayScore(score+"");
                    gridEveromentcheckMapper.updateByPrimaryKeySelective(gridEveromentcheck1);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }

}
